package com.lightkits.wms.domain.saveonhand.service.impl;

import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.database.dao.WmsOnhandQuantityMapper;
import com.lightkits.wms.database.model.BdOnhandQuantityModel;
import com.lightkits.wms.domain.saveonhand.service.QueryInventoryOnhandDomain;
import com.lightkits.wms.dto.onhand.model.BdOnhandQuantityDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * QueryInventoryOnhandDomainImpl
 *
 * @author menxiao.wu@hand-china.com
 * Created on 2019-02-27 19:51
 */
@Service
public class QueryInventoryOnhandDomainImpl implements QueryInventoryOnhandDomain {
    private final WmsOnhandQuantityMapper mapper;

    @Autowired
    public QueryInventoryOnhandDomainImpl(WmsOnhandQuantityMapper mapper) {
        this.mapper = mapper;
    }


    @Override
    public List<BdOnhandQuantityDTO> queryInventoryOnhand(List<String> organizationIdList, List<String> itemIdList, List<String> uomList,
                                                          List<String> primaryUomList, List<String> lotNumberList, List<String> warehouseIdList,
                                                          List<String> wmAreaIdList, List<String> storageUnitCodeList, List<String> ownerTypeList,
                                                          List<String> ownerIdList, List<String> featureTypeList, List<String> featureValueList,
                                                          Boolean mergeFlag) {
        List<BdOnhandQuantityModel> models = mapper.getInventoryOnhandQuantity(organizationIdList, itemIdList, uomList, primaryUomList, lotNumberList, warehouseIdList,
                wmAreaIdList, storageUnitCodeList, ownerTypeList, ownerIdList, featureTypeList, featureValueList, mergeFlag);

        return getViewDTO(models);
    }

    private List<BdOnhandQuantityDTO> getViewDTO(List<BdOnhandQuantityModel> models) {
        return ObjectUtil.isEmpty(models) ? Collections.EMPTY_LIST : models.stream().map(
                t -> {
                    BdOnhandQuantityDTO dto = new BdOnhandQuantityDTO();
//                    dto.setId(ObjectUtil.toString(t.getId()));
//                    dto.setLastUpdateDate(ObjectUtil.toString(t.getLastUpdateDate()));

                    dto.setItemId(ObjectUtil.toString(t.getItemId()));
                    dto.setItemCode(t.getItemCode());
                    dto.setItemDescription(t.getItemDescription());
                    dto.setItemType(t.getItemType());
                    dto.setCategoryName(t.getCategoryName());

                    dto.setPrimaryUom(t.getPrimaryUom());
                    dto.setPrimaryUomText(t.getPrimaryUomText());
                    dto.setQuantity(t.getQuantity());

                    dto.setWarehouseId(ObjectUtil.toString(t.getWarehouseId()));
                    dto.setWarehouseCode(t.getWarehouseCode());
                    dto.setOrganizationName(t.getOrganizationName());
                    return dto;
                }
        ).collect(Collectors.toList());
    }
}
